package mil.af.amc.adpetracker.web.actions;

import mil.af.amc.adpetracker.model.Account;
import mil.af.amc.adpetracker.model.AccountAdpeAccount;
import mil.af.amc.adpetracker.model.AdpeAccount;
import mil.af.amc.adpetracker.service.UserService;
import mil.af.amc.adpetracker.util.ForwardParameters;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */

public class LoginRoleAction extends Action {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse) throws Exception {

        SecurityContext securityContext = SecurityContextHolder.getContext();
        GrantedAuthority[] authorities = securityContext.getAuthentication().getAuthorities();

        Arrays.sort(authorities);

        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().equals("ROLE_ADMIN")) {
                // Dispatch to whatever a admin gets to see
            } else if (authority.getAuthority().equalsIgnoreCase("ROLE_EC")) {

                Account acct = (Account) securityContext.getAuthentication().getPrincipal();
                int userId = acct.getId();

                //String username = ((org.springframework.security.userdetails.User) securityContext.getAuthentication().getPrincipal()).getUsername();
                //User user = userService.getUserByUsername(username);

                // Set the user id
                //int userId = user.getId();
                httpServletRequest.getSession().setAttribute("userId", userId);

                List<AdpeAccount> adpeAccountList = new ArrayList<AdpeAccount>();
                // Set the "default" adpe account id
                for (AccountAdpeAccount aaa : acct.getAccountAdpeAccounts()) {
                    adpeAccountList.add(aaa.getAdpeAccount());
                }
                //List<AdpeAccount> userAdpeAccountList = userService.getUserAdpeAccounts(user);
                int adpeAccountId = adpeAccountList.get(0).getId();
                httpServletRequest.getSession().setAttribute("accountId", adpeAccountId);
                httpServletRequest.getSession().setAttribute("draId", adpeAccountList.get(0).getDra().getId());

                ForwardParameters fp = new ForwardParameters();
                fp.add("userId", Integer.toString(userId));

                ActionForward newForward = fp.forward(actionMapping.findForward("viewEC"));
                return newForward;
            } else {

                Account acct = (Account) securityContext.getAuthentication().getPrincipal();
                //String username = acct.getUsername();
                int userId = acct.getId();
                //String username = ((org.springframework.security.userdetails.User) securityContext.getAuthentication().getPrincipal()).getUsername();
                //User user = userService.getUserByUsername(username);
                //int userId = user.getId();
                httpServletRequest.getSession().setAttribute("userId", userId);
                return actionMapping.findForward("viewUser");

            }
        }
        return null;
    }
}
